<?php
/** 
 * 评论
 * @author Legend. <xcx_legender@qq.com>
 * @param 
 * @return 
 */

namespace Common\Service;

Class CommentsService extends BaseService
{
	/**
	 	*id, video_id, uid, status, nickname, create_time
		*
	 	*content ( 内容 非html ) 存入需要用htmlspecialchars 转
		*
	 	*referrer 评论时@的用户id对象	 
	 */

	// 状态
	const STATUS_PEDDING = 0;	// 审核 pedding
	const STATUS_PASS 	 = 1;	// 展示 pass
	const STATUS_DEL  	 = -1;  // 删除 delete

	//ERROR 错误
	const ERROR_COMMENT = '评论不能为空';

	protected $_validate = array(
		array('content', 'require', self::ERROR_COMMENT, self::MUST_VALIDATE , 'regex', self::MODEL_BOTH),
	);

	protected $_auto = array(
        array('uid', 'intval', self::MODEL_BOTH, 'function'),
        array('content', 'htmlspecialchars', self::MODEL_BOTH, 'function'),
        array('create_time', NOW_TIME, self::MODEL_INSERT),
        array('status', self::STATUS_PEDDING, self::MODEL_INSERT),
        array('zan', 0, self::MODEL_INSERT),
    );  

	/**
	*新增评论
	*@param array $comment
	*@return mixed bool|int
	*@author susy<susy.liang@ilongyuan.com.cn>
	*/
	public function addComment($comment=array()){
		return ($this->create($comment)) ? $this->add() : false;
	}

	/**
	*获取评论的明细
	*@param int $vid 评论的id
	*@return array
	*@author susy<susy.liang@ilongyuan.com.cn>
	*/
	public function detail($map=array(), $limit = '', $field=''){
		$res = $this->where($map)->order('create_time desc');
		$limit && $res = $res->limit($limit);
		return $res->select();
	}

	/**
	 * 分页方式获取评论列表
	 * @author liubo <liubo@ilongyuan.com.cn>
	 * @param  array  $condition  查询条件
	 * @param  integer $p         页码
	 * @param  integer $pager     每页数量
	 */
	public function getByPager($condition, $p = 1, $pager = 10, $order = 'create_time desc') {
		$result = $this->where($condition)->order($order)
					   ->limit(($p-1)*$pager, $pager)->select();
		return $result;
	}

	public function countByVideo($video_id)
	{
		$map = [
			'video_id' => (int)$video_id,
			'status'   => ['$gte' => self::STATUS_PEDDING],
		];
		return $this->where($map)->count();
	}




}